/*
 * FADirections.cpp
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#include "FADirections.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>

using namespace std;

FADirections::FADirections(string filename){
	ifstream in(filename.c_str());
	string line;
	while (getline(in, line)) {
		istringstream iline(line);
		string x_str, y_str, angle_str;
		getline(iline, x_str, '\t');
		getline(iline, y_str, '\t');
		getline(iline, angle_str);
		//cout << "read: " << x_str << " - " << y_str << ", " << angle_str << endl;
		Vertex v(atof(x_str.c_str()), atof(y_str.c_str()),0);
		double angle = atof(angle_str.c_str());

		positions.push_back(v);
		angles.push_back(angle);
	}
}

double FADirections::getAngleOfFAClosestTo(Vertex v){
	double distance, angle;
	distance = (v-positions[0]).abs();
	angle = angles[0];
	for(unsigned int i=1; i<positions.size();i++){
		double newdistance = (v-positions[i]).abs();
		if(newdistance < distance){
			distance = newdistance;
			angle = angles[i];
		}
	}
	return angle;
}


